

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>TocManager - LiterateCS</title>

	<link rel="icon" type="image/x-icon" href="../images/favicon.ico">
    <link rel="stylesheet" href="../bootstrap/css/readable/bootstrap.min.css" />
<link rel="stylesheet" href="../font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../sidebar/sidebar.min.css" />
<link rel="stylesheet" href="../css/book.min.css" />
<link rel="stylesheet" href="../syntax-highlight/solarized-light.min.css" />
<link rel="stylesheet" href="../mermaid/mermaid.css" />

</head>

<body>
    
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#sidebar-toggle" id="sidebar-toggle">
					<span>
						<img src="../images/favicon.ico" height="24" class="hidden-sm hidden-xs" />
						<i id="sidebar-toggle-icon" class="hidden-md hidden-lg hidden-xl fa fa-angle-double-right" aria-hidden="true"></i>
						LiterateCS
					</span>
				</a>
            </div>
            <div id="navbar" class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a href="../index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></li>
					<li><a href="https://github.com/johtela/LiterateCS"><i class="fa fa-github" aria-hidden="true"></i> GitHub Repository</a></li>
                    <li><a href="https://www.nuget.org/packages/LiterateCS/"><i class="fa fa-download" aria-hidden="true"></i> Download</a></li>
                    <li><a href="../License.html">License</a></li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container">
        <div class="row">
            <div id="sidebar" class="col-md-3 hidden-sm hidden-xs">
                
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4>On This Page</h4>
            </div>
            <div id="header-menu" class="panel-body main-menu">
                <ul></ul>
            </div>
        </div>  
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4>Table of Contents</h4>
            </div>
            <div class="panel-body main-menu">
                <ul>
	<li><a href="../index.html">Home</a></li>
	<ul>
	</ul>
	<li><a href="../Introduction.html">Introduction</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/Options.html">Command Line Options</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/Program.html">Main Program</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/SplitPath.html">SplitPath Structure</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/Weaver.html">Document Weaver</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/MdWeaver.html">Markdown Weaver</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/HtmlWeaver.html">HTML Weaver</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/TocManager.html" class="selected">TOC Manager</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/BlockList.html">Source Blocks</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/Macro.html">Macros</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/BlockBuilder.html">Block Builder</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/HtmlBlockBuilder.html">HTML Block Builder</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS/HtmlGenerator.html">HTML Generation</a></li>
	<ul>
	</ul>
	<li>Themes</li>
	<ul>
<ul>
	<li><a href="../LiterateCS.Theme/DirHelpers.html">Directory Utilities</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS.Theme/Toc.html">Table of Contents Classes</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS.Theme/PageParams.html">Page Parameters</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS.Theme/Theme.html">Theme Base Class</a></li>
	<ul>
	</ul>
	<li><a href="../LiterateCS.Theme/LiterateException.html">Reporting Errors</a></li>
	<ul>
	</ul>
</ul>
	</ul>
	<li><a href="../FrontMatter.html">Front Matter</a></li>
	<ul>
	</ul>
	<li><a href="../TableOfContents.html">Table of Contents File</a></li>
	<ul>
	</ul>
	<li><a href="../TipsAndTricks.html">Tips &amp; Tricks</a></li>
	<ul>
	</ul>
	<li><a href="../License.html">License</a></li>
	<ul>
	</ul>
	<li><a href="../README.html">README</a></li>
	<ul>
	</ul>
	<li><a href="../Installation.html">Installation</a></li>
	<ul>
	</ul>
</ul>
            </div>
        </div>

            </div>
			<div id="contentarea" class="col-md-9 col-sm-12 col-sm-push-0 col-xs-12 col-xs-push-0">
				<ul class="pager">
	<li class="previous"><a href="../LiterateCS/HtmlWeaver.html">Previous: HTML Weaver</a></li>
	<li class="next"><a href="../LiterateCS/BlockList.html">Next: Source Blocks</a></li>
</ul>
				<div id="static-content" class="markdown">
					<h1 id="toc-manager">TOC Manager</h1>
<p>Although the TOC data structure is defined in the <a href="../LiterateCS.Theme/Toc.html">LiterateCS.Theme</a>
assembly, the code that loads and saves TOC can be found here, under the TocManager class.
Themes only need to read the table of contents; loading and updating it is handled by the
main application.</p>
<h2 id="dependencies">Dependencies</h2>
<p>TocManager is a helper class that groups together the functionality related
to the file format and serialization. It uses <a href="https://github.com/aaubry/YamlDotNet">YamlDotNet</a>
library to parse and deserialize the TOC file. The format of the file is described
on a <a href="../TableOfContents.html">separate page</a>.</p>
<pre class="csharp"><code class="csharp"><span class="keyword" id="LiterateCS">namespace</span> LiterateCS
<span class="punctuation">{</span>
	<span class="keyword">using</span> LiterateCS<span class="punctuation">.</span>Theme<span class="punctuation">;</span>
	<span class="keyword">using</span> System<span class="punctuation">;</span>
	<span class="keyword">using</span> System<span class="punctuation">.</span>Collections<span class="punctuation">.</span>Generic<span class="punctuation">;</span>
	<span class="keyword">using</span> System<span class="punctuation">.</span>IO<span class="punctuation">;</span>
	<span class="keyword">using</span> System<span class="punctuation">.</span>Linq<span class="punctuation">;</span>
	<span class="keyword">using</span> System<span class="punctuation">.</span>Text<span class="punctuation">;</span>
	<span class="keyword">using</span> YamlDotNet<span class="punctuation">.</span>Serialization<span class="punctuation">;</span>
	<span class="keyword">using</span> YamlDotNet<span class="punctuation">.</span>Serialization<span class="punctuation">.</span>NamingConventions<span class="punctuation">;</span>

	<span class="keyword" id="LiterateCS.TocManager">public</span> <span class="keyword">class</span> <span class="typename">TocManager</span>
	<span class="punctuation">{</span>
</code></pre>
<h2 id="toc-lifespan">TOC Lifespan</h2>
<p>When a TocManager is created, a filename and the actual Toc object are
provided. These are kept in private fields for the other methods to access.
If new sections are added, they are be kept in a separate list to keep the
Toc	object immutable during the operation of the program.</p>
<pre class="csharp"><code class="csharp">		<span class="keyword">private</span> <span class="typename" data-toggle="tooltip" title="LiterateCS.Theme.Toc"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Toc">Toc</a></span> <span id="LiterateCS.TocManager._toc">_toc</span><span class="punctuation">;</span>
		<span class="keyword">private</span> <span class="keyword" data-toggle="tooltip" title="string">string</span> <span id="LiterateCS.TocManager._filename">_filename</span><span class="punctuation">;</span>
		<span class="keyword">private</span> <span class="typename" data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>">List</span><span class="punctuation">&lt;</span><span class="typename" data-toggle="tooltip" title="LiterateCS.Theme.Section"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section">Section</a></span><span class="punctuation">&gt;</span> <span id="LiterateCS.TocManager._addedSections">_addedSections</span><span class="punctuation">;</span>
</code></pre>
<p>The users of the class naturally need to access the Toc object. This is
possible through a read-only property.</p>
<pre class="csharp"><code class="csharp">		<span class="keyword" id="LiterateCS.TocManager.Toc">public</span> <span class="typename" data-toggle="tooltip" title="LiterateCS.Theme.Toc"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Toc">Toc</a></span> Toc <span class="punctuation">=&gt;</span> <span data-toggle="tooltip" title="LiterateCS.Theme.Toc"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._toc">_toc</a></span><span class="punctuation">;</span>
</code></pre>
<h2 id="constructing-toc-manager">Constructing TOC Manager</h2>
<p>There are two ways to create a TocManager. When a TOC file already exist, it
is loaded and initialized automatically.</p>
<pre class="csharp"><code class="csharp">		<span class="keyword" id="LiterateCS.TocManager.TocManager(LiterateCS.Theme.Toc, string)" data-toggle="tooltip" title="LiterateCS.TocManager.TocManager(LiterateCS.Theme.Toc, string)">public</span> <span data-toggle="tooltip" title="LiterateCS.TocManager.TocManager(LiterateCS.Theme.Toc, string)">TocManager</span> <span class="punctuation">(</span><span class="typename" data-toggle="tooltip" title="LiterateCS.Theme.Toc"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Toc">Toc</a></span> toc<span class="punctuation">,</span> <span class="keyword" data-toggle="tooltip" title="string">string</span> filename<span class="punctuation">)</span>
		<span class="punctuation">{</span>
			<span data-toggle="tooltip" title="LiterateCS.Theme.Toc"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._toc">_toc</a></span> <span class="punctuation" data-toggle="tooltip" title="LiterateCS.Theme.Toc">=</span> <span data-toggle="tooltip" title="LiterateCS.Theme.Toc">toc</span> <span class="punctuation" data-toggle="tooltip" title="LiterateCS.Theme.Toc">??</span> <span class="keyword">throw</span> <span class="keyword" data-toggle="tooltip" title="System.ArgumentNullException">new</span> <span class="typename">ArgumentNullException</span> <span class="punctuation">(</span><span data-toggle="tooltip" title="?">nameof</span> <span class="punctuation">(</span><span data-toggle="tooltip" title="LiterateCS.Theme.Toc">toc</span><span class="punctuation">)</span><span class="punctuation">)</span><span class="punctuation">;</span>
			<span data-toggle="tooltip" title="string"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._filename">_filename</a></span> <span class="punctuation" data-toggle="tooltip" title="string">=</span> <span data-toggle="tooltip" title="string">filename</span><span class="punctuation">;</span>
			<span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._addedSections">_addedSections</a></span> <span class="punctuation" data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>">=</span> <span class="keyword" data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>">new</span> <span class="typename">List</span><span class="punctuation">&lt;</span><span class="typename" data-toggle="tooltip" title="LiterateCS.Theme.Section"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section">Section</a></span><span class="punctuation">&gt;</span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
		<span class="punctuation">}</span>
</code></pre>
<p>If there is no file available, a parameterless constructor can be called to
create an empty TOC. In this case the TOC does not need to be updated, so
the <code>_addedSections</code> field can be left uninitialized.</p>
<pre class="csharp"><code class="csharp">		<span class="keyword" id="LiterateCS.TocManager.TocManager()" data-toggle="tooltip" title="LiterateCS.TocManager.TocManager()">public</span> <span data-toggle="tooltip" title="LiterateCS.TocManager.TocManager()">TocManager</span> <span class="punctuation">(</span><span class="punctuation">)</span>
		<span class="punctuation">{</span>
			<span data-toggle="tooltip" title="LiterateCS.Theme.Toc"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._toc">_toc</a></span> <span class="punctuation" data-toggle="tooltip" title="LiterateCS.Theme.Toc">=</span> <span class="keyword" data-toggle="tooltip" title="LiterateCS.Theme.Toc">new</span> <span class="typename"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Toc">Toc</a></span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
			<span data-toggle="tooltip" title="LiterateCS.Theme.Toc"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._toc">_toc</a></span><span class="punctuation" data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>">.</span><span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Toc.Contents">Contents</a></span> <span class="punctuation" data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>">=</span> <span class="keyword" data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>">new</span> <span class="typename">List</span><span class="punctuation">&lt;</span><span class="typename" data-toggle="tooltip" title="LiterateCS.Theme.Section"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section">Section</a></span><span class="punctuation">&gt;</span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
		<span class="punctuation">}</span>
</code></pre>
<h2 id="loading-toc">Loading TOC</h2>
<p>TOC file is loaded using the deserialization feature provided by YamlDotNet.
We configure the deserializer so that it converts the names of the data items
into camel case and then maps them to the properties of the Toc class. Then
we just open the file and feed it to the deserializer.</p>
<pre class="csharp"><code class="csharp">		<span class="keyword" id="LiterateCS.TocManager.Load(string)" data-toggle="tooltip" title="LiterateCS.TocManager.Load(string)">public</span> <span class="keyword" data-toggle="tooltip" title="LiterateCS.TocManager.Load(string)">static</span> <span class="typename" data-toggle="tooltip" title="LiterateCS.TocManager"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager">TocManager</a></span> <span data-toggle="tooltip" title="LiterateCS.TocManager.Load(string)">Load</span> <span class="punctuation">(</span><span class="keyword" data-toggle="tooltip" title="string">string</span> yamlFile<span class="punctuation">)</span>
		<span class="punctuation">{</span>
			<span class="keyword" data-toggle="tooltip" title="YamlDotNet.Serialization.IDeserializer">var</span> <span id="deserializer">deserializer</span> <span class="punctuation">=</span> <span class="keyword" data-toggle="tooltip" title="YamlDotNet.Serialization.DeserializerBuilder">new</span> <span class="typename">DeserializerBuilder</span> <span class="punctuation">(</span><span class="punctuation">)</span>
				<span class="punctuation" data-toggle="tooltip" title="YamlDotNet.Serialization.BuilderSkeleton<YamlDotNet.Serialization.DeserializerBuilder>.WithNamingConvention(YamlDotNet.Serialization.INamingConvention)">.</span><span data-toggle="tooltip" title="YamlDotNet.Serialization.BuilderSkeleton<YamlDotNet.Serialization.DeserializerBuilder>.WithNamingConvention(YamlDotNet.Serialization.INamingConvention)">WithNamingConvention</span> <span class="punctuation">(</span><span class="keyword" data-toggle="tooltip" title="YamlDotNet.Serialization.NamingConventions.CamelCaseNamingConvention">new</span> <span class="typename">CamelCaseNamingConvention</span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">)</span>
				<span class="punctuation" data-toggle="tooltip" title="YamlDotNet.Serialization.DeserializerBuilder.Build()">.</span><span data-toggle="tooltip" title="YamlDotNet.Serialization.DeserializerBuilder.Build()">Build</span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
			<span class="keyword">try</span>
			<span class="punctuation">{</span>
				<span class="keyword">using</span> <span class="punctuation">(</span><span class="keyword" data-toggle="tooltip" title="System.IO.StreamReader">var</span> <span id="input">input</span> <span class="punctuation">=</span> <span class="typename" data-toggle="tooltip" title="System.IO.File">File</span><span class="punctuation" data-toggle="tooltip" title="System.IO.File.OpenText(string)">.</span><span data-toggle="tooltip" title="System.IO.File.OpenText(string)">OpenText</span> <span class="punctuation">(</span><span data-toggle="tooltip" title="string">yamlFile</span><span class="punctuation">)</span><span class="punctuation">)</span>
					<span class="keyword">return</span> <span class="keyword" data-toggle="tooltip" title="LiterateCS.TocManager">new</span> <span class="typename"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager">TocManager</a></span> <span class="punctuation">(</span><span data-toggle="tooltip" title="YamlDotNet.Serialization.IDeserializer">deserializer</span><span class="punctuation" data-toggle="tooltip" title="YamlDotNet.Serialization.IDeserializer.Deserialize<LiterateCS.Theme.Toc>(System.IO.TextReader)">.</span><span data-toggle="tooltip" title="YamlDotNet.Serialization.IDeserializer.Deserialize<LiterateCS.Theme.Toc>(System.IO.TextReader)">Deserialize</span><span class="punctuation">&lt;</span><span class="typename" data-toggle="tooltip" title="LiterateCS.Theme.Toc"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Toc">Toc</a></span><span class="punctuation">&gt;</span> <span class="punctuation">(</span><span data-toggle="tooltip" title="System.IO.StreamReader">input</span><span class="punctuation">)</span><span class="punctuation">,</span> 
						<span data-toggle="tooltip" title="string">yamlFile</span><span class="punctuation">)</span><span class="punctuation">;</span>
			<span class="punctuation">}</span>
</code></pre>
<p>The most complicated part of the loading is the error reporting,
which tries to give as much information for the error as possible.</p>
<pre class="csharp"><code class="csharp">			<span class="keyword">catch</span> <span class="punctuation">(</span><span class="typename" data-toggle="tooltip" title="System.Exception">Exception</span> e<span class="punctuation">)</span>
			<span class="punctuation">{</span>
				<span class="keyword">throw</span> <span class="keyword" data-toggle="tooltip" title="LiterateCS.Theme.LiterateException">new</span> <span class="typename"><a href="../LiterateCS.Theme/LiterateException.html#LiterateCS.Theme.LiterateException">LiterateException</a></span> <span class="punctuation">(</span><span class="string" data-toggle="tooltip" title="string">&quot;Could not load TOC file. &quot;</span> <span class="punctuation" data-toggle="tooltip" title="string">+</span>
					<span class="string" data-toggle="tooltip" title="string">&quot;Make sure that its format is correct&quot;</span><span class="punctuation">,</span> <span data-toggle="tooltip" title="string">yamlFile</span><span class="punctuation">,</span>
					<span class="string" data-toggle="tooltip" title="string">&quot;https://johtela.github.io/LiterateCS/TableOfContents.html&quot;</span><span class="punctuation">,</span> <span data-toggle="tooltip" title="System.Exception">e</span><span class="punctuation">)</span><span class="punctuation">;</span>
			<span class="punctuation">}</span>
		<span class="punctuation">}</span>
</code></pre>
<h2 id="adding-new-sections">Adding New Sections</h2>
<p>Instead of adding a new entry to TOC while the documentation is being
generated, we add it to a temporary list. This list is concatenated
to the TOC before it is saved. If there is no file associated to the
TOC (in which case the temporary list is null), then we don't need to
do anything.</p>
<pre class="csharp"><code class="csharp">		<span class="keyword" id="LiterateCS.TocManager.AddToToc(LiterateCS.SplitPath)" data-toggle="tooltip" title="LiterateCS.TocManager.AddToToc(LiterateCS.SplitPath)">public</span> <span class="keyword" data-toggle="tooltip" title="void">void</span> <span data-toggle="tooltip" title="LiterateCS.TocManager.AddToToc(LiterateCS.SplitPath)">AddToToc</span> <span class="punctuation">(</span><span class="typename" data-toggle="tooltip" title="LiterateCS.SplitPath"><a href="../LiterateCS/SplitPath.html#LiterateCS.SplitPath">SplitPath</a></span> path<span class="punctuation">)</span>
		<span class="punctuation">{</span>
			<span class="keyword">if</span> <span class="punctuation">(</span><span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._addedSections">_addedSections</a></span> <span class="punctuation" data-toggle="tooltip" title="bool">==</span> <span class="keyword">null</span><span class="punctuation">)</span>
				<span class="keyword">return</span><span class="punctuation">;</span>
			<span class="keyword" data-toggle="tooltip" title="LiterateCS.Theme.Section">var</span> <span id="section">section</span> <span class="punctuation">=</span> <span data-toggle="tooltip" title="LiterateCS.TocManager.FindSectionForFile(string, System.Collections.Generic.List<LiterateCS.Theme.Section>)"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager.FindSectionForFile(string, System.Collections.Generic.List<LiterateCS.Theme.Section>)">FindSectionForFile</a></span> <span class="punctuation">(</span><span data-toggle="tooltip" title="LiterateCS.SplitPath">path</span><span class="punctuation" data-toggle="tooltip" title="string">.</span><span data-toggle="tooltip" title="string"><a href="../LiterateCS/SplitPath.html#LiterateCS.SplitPath.FilePath">FilePath</a></span><span class="punctuation">,</span> <span data-toggle="tooltip" title="LiterateCS.Theme.Toc"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager.Toc">Toc</a></span><span class="punctuation" data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>">.</span><span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Toc.Contents">Contents</a></span><span class="punctuation">)</span> <span class="punctuation" data-toggle="tooltip" title="LiterateCS.Theme.Section">??</span>
				<span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._addedSections">_addedSections</a></span><span class="punctuation" data-toggle="tooltip" title="System.Collections.Generic.IEnumerable<LiterateCS.Theme.Section>.FirstOrDefault<LiterateCS.Theme.Section>(System.Func<LiterateCS.Theme.Section, bool>)">.</span><span data-toggle="tooltip" title="System.Collections.Generic.IEnumerable<LiterateCS.Theme.Section>.FirstOrDefault<LiterateCS.Theme.Section>(System.Func<LiterateCS.Theme.Section, bool>)">FirstOrDefault</span> <span class="punctuation">(</span>s <span class="punctuation" data-toggle="tooltip" title="lambda expression">=&gt;</span> <span data-toggle="tooltip" title="LiterateCS.Theme.Section">s</span><span class="punctuation" data-toggle="tooltip" title="string">.</span><span data-toggle="tooltip" title="string"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section.File">File</a></span> <span class="punctuation" data-toggle="tooltip" title="bool">==</span> <span data-toggle="tooltip" title="LiterateCS.SplitPath">path</span><span class="punctuation" data-toggle="tooltip" title="string">.</span><span data-toggle="tooltip" title="string"><a href="../LiterateCS/SplitPath.html#LiterateCS.SplitPath.FilePath">FilePath</a></span><span class="punctuation">)</span><span class="punctuation">;</span>
			<span class="keyword">if</span> <span class="punctuation">(</span><span data-toggle="tooltip" title="LiterateCS.Theme.Section">section</span> <span class="punctuation" data-toggle="tooltip" title="bool">==</span> <span class="keyword">null</span><span class="punctuation">)</span>
			<span class="punctuation">{</span>
				<span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._addedSections">_addedSections</a></span><span class="punctuation" data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>.Add(LiterateCS.Theme.Section)">.</span><span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>.Add(LiterateCS.Theme.Section)">Add</span> <span class="punctuation">(</span><span class="keyword" data-toggle="tooltip" title="LiterateCS.Theme.Section">new</span> <span class="typename"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section">Section</a></span> <span class="punctuation">(</span><span class="punctuation">)</span>
				<span class="punctuation">{</span>
					<span data-toggle="tooltip" title="string"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section.Page">Page</a></span> <span class="punctuation" data-toggle="tooltip" title="string">=</span> <span data-toggle="tooltip" title="LiterateCS.SplitPath">path</span><span class="punctuation" data-toggle="tooltip" title="string">.</span><span data-toggle="tooltip" title="string"><a href="../LiterateCS/SplitPath.html#LiterateCS.SplitPath.FileNameWithoutExtension">FileNameWithoutExtension</a></span><span class="punctuation">,</span>
					<span data-toggle="tooltip" title="string"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section.File">File</a></span> <span class="punctuation" data-toggle="tooltip" title="string">=</span> <span data-toggle="tooltip" title="LiterateCS.SplitPath">path</span><span class="punctuation" data-toggle="tooltip" title="string">.</span><span data-toggle="tooltip" title="string"><a href="../LiterateCS/SplitPath.html#LiterateCS.SplitPath.FilePath">FilePath</a></span><span class="punctuation">,</span>
					<span data-toggle="tooltip" title="string"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section.Desc">Desc</a></span> <span class="punctuation" data-toggle="tooltip" title="string">=</span> <span class="string" data-toggle="tooltip" title="string">&quot;TODO! Add page description.&quot;</span>
				<span class="punctuation">}</span><span class="punctuation">)</span><span class="punctuation">;</span>
			<span class="punctuation">}</span>
		<span class="punctuation">}</span>
</code></pre>
<h2 id="saving-toc">Saving TOC</h2>
<p>We could use the serialization functionality of YamlDotNet to write the TOC
back to a file, but it is actually simpler just add the new entries as text.
Serialization has its own quirks which we should work around, if we would
like to use it. The new entries are always at the end of the file, so we
can just open the file in the append mode and write the sections into it.</p>
<pre class="csharp"><code class="csharp">		<span class="keyword" id="LiterateCS.TocManager.Save()" data-toggle="tooltip" title="LiterateCS.TocManager.Save()">public</span> <span class="keyword" data-toggle="tooltip" title="void">void</span> <span data-toggle="tooltip" title="LiterateCS.TocManager.Save()">Save</span> <span class="punctuation">(</span><span class="punctuation">)</span>
		<span class="punctuation">{</span>
			<span class="keyword">if</span> <span class="punctuation">(</span><span data-toggle="tooltip" title="string"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._filename">_filename</a></span> <span class="punctuation" data-toggle="tooltip" title="bool">==</span> <span class="keyword">null</span> <span class="punctuation" data-toggle="tooltip" title="bool">||</span> <span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._addedSections">_addedSections</a></span> <span class="punctuation" data-toggle="tooltip" title="bool">==</span> <span class="keyword">null</span> <span class="punctuation" data-toggle="tooltip" title="bool">||</span> 
				<span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._addedSections">_addedSections</a></span><span class="punctuation" data-toggle="tooltip" title="int">.</span><span data-toggle="tooltip" title="int">Count</span> <span class="punctuation" data-toggle="tooltip" title="bool">==</span> <span class="number" data-toggle="tooltip" title="int">0</span><span class="punctuation">)</span>
				<span class="keyword">return</span><span class="punctuation">;</span>
			<span class="keyword">using</span> <span class="punctuation">(</span><span class="keyword" data-toggle="tooltip" title="System.IO.StreamWriter">var</span> <span id="output">output</span> <span class="punctuation">=</span> <span class="typename" data-toggle="tooltip" title="System.IO.File">File</span><span class="punctuation" data-toggle="tooltip" title="System.IO.File.AppendText(string)">.</span><span data-toggle="tooltip" title="System.IO.File.AppendText(string)">AppendText</span> <span class="punctuation">(</span><span data-toggle="tooltip" title="string"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._filename">_filename</a></span><span class="punctuation">)</span><span class="punctuation">)</span>
				<span class="keyword">foreach</span> <span class="punctuation">(</span><span class="keyword" data-toggle="tooltip" title="LiterateCS.Theme.Section">var</span> section <span class="keyword">in</span> <span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager._addedSections">_addedSections</a></span><span class="punctuation">)</span>
				<span class="punctuation">{</span>
					<span data-toggle="tooltip" title="System.IO.StreamWriter">output</span><span class="punctuation" data-toggle="tooltip" title="System.IO.TextWriter.WriteLine()">.</span><span data-toggle="tooltip" title="System.IO.TextWriter.WriteLine()">WriteLine</span> <span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
					<span data-toggle="tooltip" title="System.IO.StreamWriter">output</span><span class="punctuation" data-toggle="tooltip" title="System.IO.StreamWriter.WriteLine(string)">.</span><span data-toggle="tooltip" title="System.IO.StreamWriter.WriteLine(string)">WriteLine</span> <span class="punctuation">(</span><span class="string" data-toggle="tooltip" title="string">&quot;  - page: &quot;</span> <span class="punctuation" data-toggle="tooltip" title="string">+</span> <span data-toggle="tooltip" title="LiterateCS.Theme.Section">section</span><span class="punctuation" data-toggle="tooltip" title="string">.</span><span data-toggle="tooltip" title="string"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section.Page">Page</a></span><span class="punctuation">)</span><span class="punctuation">;</span>
					<span data-toggle="tooltip" title="System.IO.StreamWriter">output</span><span class="punctuation" data-toggle="tooltip" title="System.IO.StreamWriter.WriteLine(string)">.</span><span data-toggle="tooltip" title="System.IO.StreamWriter.WriteLine(string)">WriteLine</span> <span class="punctuation">(</span><span class="string" data-toggle="tooltip" title="string">&quot;    file: &quot;</span> <span class="punctuation" data-toggle="tooltip" title="string">+</span> <span data-toggle="tooltip" title="LiterateCS.Theme.Section">section</span><span class="punctuation" data-toggle="tooltip" title="string">.</span><span data-toggle="tooltip" title="string"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section.File">File</a></span><span class="punctuation">)</span><span class="punctuation">;</span>
					<span data-toggle="tooltip" title="System.IO.StreamWriter">output</span><span class="punctuation" data-toggle="tooltip" title="System.IO.StreamWriter.WriteLine(string)">.</span><span data-toggle="tooltip" title="System.IO.StreamWriter.WriteLine(string)">WriteLine</span> <span class="punctuation">(</span><span class="string" data-toggle="tooltip" title="string">&quot;    desc: &quot;</span> <span class="punctuation" data-toggle="tooltip" title="string">+</span> <span data-toggle="tooltip" title="LiterateCS.Theme.Section">section</span><span class="punctuation" data-toggle="tooltip" title="string">.</span><span data-toggle="tooltip" title="string"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section.Desc">Desc</a></span><span class="punctuation">)</span><span class="punctuation">;</span>
				<span class="punctuation">}</span>
		<span class="punctuation">}</span>
</code></pre>
<h2 id="checking-if-a-section-exists">Checking If a Section Exists</h2>
<p>To check whether a file is not in TOC already we need to traverse the TOC tree
recursively. The function below does just that. If a given file is not found
in the tree, <code>null</code> is returned.</p>
<pre class="csharp"><code class="csharp">		<span class="keyword" id="LiterateCS.TocManager.FindSectionForFile(string, System.Collections.Generic.List<LiterateCS.Theme.Section>)" data-toggle="tooltip" title="LiterateCS.TocManager.FindSectionForFile(string, System.Collections.Generic.List<LiterateCS.Theme.Section>)">public</span> <span class="keyword" data-toggle="tooltip" title="LiterateCS.TocManager.FindSectionForFile(string, System.Collections.Generic.List<LiterateCS.Theme.Section>)">static</span> <span class="typename" data-toggle="tooltip" title="LiterateCS.Theme.Section"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section">Section</a></span> <span data-toggle="tooltip" title="LiterateCS.TocManager.FindSectionForFile(string, System.Collections.Generic.List<LiterateCS.Theme.Section>)">FindSectionForFile</span> <span class="punctuation">(</span><span class="keyword" data-toggle="tooltip" title="string">string</span> file<span class="punctuation">,</span> <span class="typename" data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>">List</span><span class="punctuation">&lt;</span><span class="typename" data-toggle="tooltip" title="LiterateCS.Theme.Section"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section">Section</a></span><span class="punctuation">&gt;</span> sections<span class="punctuation">)</span>
		<span class="punctuation">{</span>
			<span class="keyword">foreach</span> <span class="punctuation">(</span><span class="keyword" data-toggle="tooltip" title="LiterateCS.Theme.Section">var</span> section <span class="keyword">in</span> <span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>">sections</span><span class="punctuation">)</span>
			<span class="punctuation">{</span>
				<span class="keyword">if</span> <span class="punctuation">(</span><span data-toggle="tooltip" title="LiterateCS.Theme.Section">section</span><span class="punctuation" data-toggle="tooltip" title="string">.</span><span data-toggle="tooltip" title="string"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section.File">File</a></span> <span class="punctuation" data-toggle="tooltip" title="bool">==</span> <span data-toggle="tooltip" title="string">file</span><span class="punctuation">)</span>
					<span class="keyword">return</span> <span data-toggle="tooltip" title="LiterateCS.Theme.Section">section</span><span class="punctuation">;</span>
				<span class="keyword">if</span> <span class="punctuation">(</span><span data-toggle="tooltip" title="LiterateCS.Theme.Section">section</span><span class="punctuation" data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>">.</span><span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section.Subs">Subs</a></span> <span class="punctuation" data-toggle="tooltip" title="bool">!=</span> <span class="keyword">null</span><span class="punctuation">)</span>
				<span class="punctuation">{</span>
					<span class="keyword" data-toggle="tooltip" title="LiterateCS.Theme.Section">var</span> <span id="subSection">subSection</span> <span class="punctuation">=</span> <span data-toggle="tooltip" title="LiterateCS.TocManager.FindSectionForFile(string, System.Collections.Generic.List<LiterateCS.Theme.Section>)"><a href="../LiterateCS/TocManager.html#LiterateCS.TocManager.FindSectionForFile(string, System.Collections.Generic.List<LiterateCS.Theme.Section>)">FindSectionForFile</a></span> <span class="punctuation">(</span><span data-toggle="tooltip" title="string">file</span><span class="punctuation">,</span> <span data-toggle="tooltip" title="LiterateCS.Theme.Section">section</span><span class="punctuation" data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>">.</span><span data-toggle="tooltip" title="System.Collections.Generic.List<LiterateCS.Theme.Section>"><a href="../LiterateCS.Theme/Toc.html#LiterateCS.Theme.Section.Subs">Subs</a></span><span class="punctuation">)</span><span class="punctuation">;</span>
					<span class="keyword">if</span> <span class="punctuation">(</span><span data-toggle="tooltip" title="LiterateCS.Theme.Section">subSection</span> <span class="punctuation" data-toggle="tooltip" title="bool">!=</span> <span class="keyword">null</span><span class="punctuation">)</span>
						<span class="keyword">return</span> <span data-toggle="tooltip" title="LiterateCS.Theme.Section">subSection</span><span class="punctuation">;</span>
				<span class="punctuation">}</span>
			<span class="punctuation">}</span>
			<span class="keyword">return</span> <span class="keyword">null</span><span class="punctuation">;</span>
		<span class="punctuation">}</span>
	<span class="punctuation">}</span>
<span class="punctuation">}</span>
</code></pre>

				</div>
				<ul class="pager">
	<li class="previous"><a href="../LiterateCS/HtmlWeaver.html">Previous: HTML Weaver</a></li>
	<li class="next"><a href="../LiterateCS/BlockList.html">Next: Source Blocks</a></li>
</ul>
			</div>
        </div>
    </div>
    
    <footer class="panel-footer text-center">
        <div align="center">Copyright © 2018 Tommi Johtela</div>
		<div align="right">
			<small>
				Documentation created with <a href="https://johtela.github.io/LiterateCS/">LiterateCS</a>.
			</small>
		</div>
    </footer>


    
    <script src="../bootstrap/js/jquery.min.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    <script src="../sidebar/sidebar.js"></script>
    <script src="../syntax-highlight/syntax.js"></script>
    <script type="text/x-mathjax-config">
        MathJax.Hub.Config({
        extensions: ["jsMath2jax.js"]
        });
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML'></script>
	<script src="../mermaid/mermaid.min.js"></script>
	<script>mermaid.initialize({startOnLoad:true});</script>

</body>
</html>